Skip to content

Fix union equivalence schema rewrite with stale constants - #23375

Merged
xudong963 merged 1 commit into
apache:mainfrom
xudong963:xudong963/fix-union-equivalence-stale-constants
Jul 9, 2026
Merged

Fix union equivalence schema rewrite with stale constants#23375
xudong963 merged 1 commit into
apache:mainfrom
xudong963:xudong963/fix-union-equivalence-stale-constants

Conversation

@xudong963

@xudong963 xudong963 commented Jul 7, 2026

Copy link
Copy Markdown
Member

Which issue does this PR close?

Rationale for this change

UnionExec::try_new can panic while computing equivalence properties if stale constant metadata is carried across a projection and then rewritten to the union output schema.

In the observed shape, a filter such as ticker = 'ESU6' can leave a uniform string constant in equivalence properties. After a parent projection drops ticker, union property schema rewriting can see the remaining column slot as a timestamp column and attempt to cast 'ESU6' to Timestamp, which fails during planning.

Equivalence constants are optimizer metadata, so an unrepresentable constant after schema rewrite should be discarded rather than failing query planning.

What changes are included in this PR?

  • Drops a uniform constant during EquivalenceProperties::with_new_schema if its value cannot be cast to the rewritten expression type.
  • Removes trivial equivalence classes after dropping such constants.
  • Propagates UnionExec::compute_properties errors from UnionExec::try_new instead of unwrapping.
  • Adds a regression test for union equivalence schema rewrite with an unrepresentable stale constant value.

Are these changes tested?

Yes:

Are there any user-facing changes?

No API change. This prevents a planner panic for affected UNION ALL + filter + projection query shapes.

@github-actions github-actions Bot added physical-expr Changes to the physical-expr crates physical-plan Changes to the physical-plan crate labels Jul 7, 2026

@kosiew kosiew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xudong963
Thanks for the targeted fix.
The unit test covers the stale metadata case well, and I have one small follow-up suggestion that is not blocking.

}

#[test]
fn test_union_drops_unrepresentable_constant_value_after_schema_rewrite() -> Result<()>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice targeted test for the stale metadata state. As a follow-up, it may be worth adding a higher-level regression test that builds the filter/projection/union plan shape from the original issue and checks that UnionExec::try_new or physical planning succeeds. That would help protect the full path that produced the stale constant, not just the calculate_union boundary.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that makes sense. The current test intentionally targets the exact stale-metadata boundary in calculate_union. I agree a higher-level regression would be useful, especially if we can capture the original filter/projection/union shape reliably. Since the reduced issue does not include a concrete SQL repro and the normal projection path should drop removed-column constants, I’ll keep this PR focused and consider adding a UnionExec::try_new/planner-level regression as a follow-up.

@xudong963
xudong963 added this pull request to the merge queue Jul 9, 2026
Merged via the queue into apache:main with commit 215ebb8 Jul 9, 2026
40 checks passed
@xudong963
xudong963 deleted the xudong963/fix-union-equivalence-stale-constants branch July 9, 2026 07:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

physical-expr Changes to the physical-expr crates physical-plan Changes to the physical-plan crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UnionExec can panic when stale equivalence constants are rewritten after projection

2 participants